﻿package controller;

import bean.*;
import biz.CourseBiz;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.PreAddCallback;

import java.util.List;

@Controller
@RequestMapping("/cs")
public class CourseController {
    @Autowired
    private CourseBiz cb;

    //有用户激活，将其课程信息等存入CourseUserList表中
    @RequestMapping(value ="/insertdata")
    @ResponseBody
    public void insertData(Integer usersId){
        //根据用户找到所在公司
         int companyid=cb.findCompaneId(usersId);
         //查询其对应的课程，去课程表
         List<CourseInfo> list=cb.findAllCourse(companyid);
        for (CourseInfo c:list) {
             int courseid=c.getCourseId();  //拿到课程id，去找其对应的目录
             List<courseDetailInfo> lists=cb.findCourseDetail(courseid);
             for(courseDetailInfo cd:lists){
                 int courseCatalogueid=cd.getCourseCatalogueId(); //找到某个课程对应的几个目录，将其插入
                 CourseTest cct=new CourseTest();
                 cct.setCourseId(courseid);
                 cct.setUsersId(usersId);
                 cct.setCourseCatalogueId(courseCatalogueid);
                 cb.InsertData(cct);
             }
        }
    }

    //课程表：显示课程信息
    @RequestMapping(value = "/findallcourse")
    @ResponseBody
    public String findallcourse(Integer usersId,String callback) {
        int companyid=cb.findCompaneId(usersId);
        String str= JSONArray.toJSONString(cb.findAllCourse(companyid));
        return PreAddCallback.preAddCallback(str,callback);
    }

    //详情表：+更新课程的学习时间
    @RequestMapping(value = "/findcoursedetail")
    @ResponseBody
    public String  findcoursedetail(Integer courseId,Integer usersId,String time,String callback) {
        //将此课程的学习时间存入表中
        CourseUserList ci = new CourseUserList();
        ci.setCourseId(courseId);
        ci.setUsersId(usersId);
        ci.setCourseLearningTime(time);
        cb.StorageTime(ci);   //更新学习时间
        String str=JSONArray.toJSONString(cb.findCourseDetail(courseId));//在培训显示详细信息
        return PreAddCallback.preAddCallback (str,callback);
    }

    //点击完成课程的时候，一门课程学习完成，将其状态改为1
    @RequestMapping(value="/completecourse")
    @ResponseBody
    public void CompleteCourse(Integer courseId, Integer usersId,String type){
        if("完成课程".equals(type)){
            //调用修改状态的语句
            CourseUserList ci = new CourseUserList();
            ci.setCourseId(courseId);
            ci.setUsersId(usersId);
            cb.updateCourseState(ci);
        }
    }

    //我的已完成课程个数与所有课程个数的百分比（首页显示）
    @RequestMapping(value = "/coursecompletionstatus" )
    @ResponseBody
    public String CourseCompletionStatus(Integer usersId,String callback) {
        double allcount = cb.findAllDoneCount(usersId);
        String str = null;
        double alreadydo = cb.findIsAllDoneCount(usersId);  //已完成的
        if ((int) alreadydo == 0) {
            str="此用户暂时没有课程";
        } else {
            str=(int) ((allcount/alreadydo)*100) + "%";  //返回百分比
        }
        str=JSONArray.toJSONString(str);//在培训显示详细信息
        return PreAddCallback.preAddCallback (str,callback);
    }

    //最近一次的学习显示
    @RequestMapping(value = "/recentlearning")
    @ResponseBody
    public String recentLearning(Integer usersId,String callback) {
        String str=JSONArray.toJSONString(cb.recentLearning(usersId));
        return PreAddCallback.preAddCallback (str,callback);
    }

    //是否按顺序播放
    @RequestMapping(value = "/playsequentially" )
    @ResponseBody
    public String playSequentially(Integer courseId,Integer usersId,String callback) {
         CourseUserList ci=new CourseUserList();
         ci.setUsersId(usersId);
         ci.setCourseId(courseId);
        //此课程完成就可以任意播放
        List<CourseUserList> CourseList=cb.findOneCourse(ci);
        String str=null;
        if (CourseList.get(0).getIsAllDone()==1) {
            str="1"; //完成,可以顺序播放
        } else {
            str="0"; //不可顺序播放
        }
         str= JSONArray.toJSONString(str);
        return PreAddCallback.preAddCallback(str,callback);
    }

    //看是文档还是目录：看完一个目录，将其状态改为1(判断此视频进度是否为100%或者大于90%)
    @RequestMapping(value="/catalogue")
    @ResponseBody
    public String CataLogue(Integer courseId,Integer usersId,Integer courseCatalogueId,String type,String callback) {
        CourseUserList cul = new CourseUserList();
        cul.setCourseId(courseId);
        cul.setUsersId(usersId);
        cul.setCourseCatalogueId(courseCatalogueId);
        List<CourseUserList> detailList = cb.findOneCourse1(cul);

        courseDetailInfo cdi = new courseDetailInfo();
        cdi.setCourseId(courseId);
        cdi.setCourseCatalogueId(courseCatalogueId);
        List<courseDetailInfo> list = cb.CourseCatalogue(cdi);
        String str=null;

        if (type.equals("文档")) {
            //观看文档
            str= JSONArray.toJSONString(list.get(0).getDocument());
            return PreAddCallback.preAddCallback(str,callback);  //返回文档路径
        } else {
            str=JSONArray.toJSONString(list.get(0).getVideo());
            return PreAddCallback.preAddCallback(str,callback); //观看视频//给视频的链接（没看完或已经看完）
        }
    }

    //返回观看进度
    @RequestMapping(value="/findVideoprogress")
    @ResponseBody
    public String findVideoProgress(Integer courseId,Integer usersId,Integer courseCatalogueId,String callback){
               CourseUserList cul = new CourseUserList();
               cul.setCourseId(courseId);
               cul.setUsersId(usersId);
               cul.setCourseCatalogueId(courseCatalogueId);
               List<CourseUserList> detailList = cb.findOneCourse1(cul);

               String str=detailList.get(0).getVideoProgress();
               String string=null;

               if(str==null){
                    string= JSONArray.toJSONString("0.0");
                    return PreAddCallback.preAddCallback(string,callback); //首次观看
               }
               else {
                   double progress = Double.parseDouble(detailList.get(0).getVideoProgress());
                   if (progress > 0.9) {  //观看完成
                       CourseUserList cui = new CourseUserList();
                       cui.setCourseId(courseId); //课程id
                       cui.setCourseCatalogueId(courseCatalogueId); //目录id
                       cui.setUsersId(usersId);
                       cb.updateCataloguetate(cui);
                   }
                   string=JSONArray.toJSONString(detailList.get(0).getVideoProgress());
                   return PreAddCallback.preAddCallback(string,callback); //首次观看
               }
    }

     //得到视频进度时间,计算进度百分比
     @RequestMapping(value = "/videoprogresstime" )
     @ResponseBody
     public void VideoProgressTime(Integer usersId,Integer courseId,Integer courseCatalogueId,String videoProgress) {
               CourseUserList cul=new CourseUserList();
               cul.setUsersId(usersId);
               cul.setCourseId(courseId);
               cul.setCourseCatalogueId(courseCatalogueId);
               //总时长
               courseDetailInfo cdlf=new courseDetailInfo();
               cdlf.setCourseId(courseId);
               cdlf.setCourseCatalogueId(courseCatalogueId);

               List<courseDetailInfo> cdi=cb.CourseCatalogue(cdlf);
               String alltime=cdi.get(0).getAllTime(); //得到总时长
                double videoprogress=Double.parseDouble(videoProgress);
                double alltime1=Double.parseDouble(alltime);
                String str=(videoprogress/alltime1)+"";
                str=str.substring(0,3);

                cul.setVideoProgress(str);
                cb.updateCataLOgueTime(cul);
     }

     //查找此用户的所有课程对应的课程名：
     @RequestMapping(value = "/coursnamefind" )
     @ResponseBody
     public String CoursNamefind(Integer usersId,Integer studyGroupNumber,String callback){
         StudygroupnumberInfo sgi=new StudygroupnumberInfo();
         sgi.setUsersId(usersId);
         sgi.setStudyGroupNumber(studyGroupNumber);
         String name=cb.findStudyGroupName(sgi);

         String string=JSONArray.toJSONString(name);
         return PreAddCallback.preAddCallback(string,callback); //返回学组名
     }

    @RequestMapping(value = "/coursnamefind")
    public String CoursNamefind(Integer usersId,String callback){
        int companyid=cb.findCompaneId(usersId);
        String name=cb.findCompanyName(companyid);

        String string=JSONArray.toJSONString(name);
        return PreAddCallback.preAddCallback(string,callback); //返回公司名
    }

}
